﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DimsMapper.Modules
{
    static class MsgFiles
    {
        static readonly string[] msgPathFiles = {
            "text\\{0}\\game\\pro_item.msg",
            "text\\{0}\\game\\pro_crit.msg",
            "text\\{0}\\game\\pro_scen.msg",
            "text\\{0}\\game\\pro_wall.msg",
            "text\\{0}\\game\\pro_tile.msg",
            "text\\{0}\\game\\pro_misc.msg"
        };

        static Dictionary<int, string>[] msgContent = new Dictionary<int, string>[6];
        
        public static void LoadMessages()
        {
            for (int i = 0; i < 6; i++)
            {
                if (msgContent[i] == null) msgContent[i] = new Dictionary<int, string>();

                List<string> file = DatFiles.ReadFile(string.Format(msgPathFiles[i], Settings.LanguageFolder), Settings.encoding);
                if (file == null) continue;

                // распарсить на строки {}{}{}
                foreach (string line in file)
                {
                    if (line.StartsWith("#")) continue;

                    string[] lines = line.Split('}');
                    if (lines.Length < 3) continue;

                    int msgID = int.Parse(lines[0].Substring(1).Trim());
                    string message = lines[2].TrimStart('{').Trim();

                    if (!msgContent[i].ContainsKey(msgID)) msgContent[i].Add(msgID, message);
                }
            }
        }

        public static string GetMsg(Object.Types type, int msgNum)
        {
            if (type > Object.Types.Misc) type = Object.Types.Item;

            string msg;
            if (msgContent[(int)type].TryGetValue(msgNum, out msg)) {
                // добавить конвертирование для LK

                return msg;
            }
            return "Error";
        }

        public static void Clear()
        {
            for (int i = 0; i < 6; i++)
            {
                msgContent[i].Clear();
            }
        }
    }
}
